package com.xx.happychatboot.middleware.kaptcha.controller;

import com.google.code.kaptcha.Producer;
import com.xx.happychatboot.base.vo.response.ResponseDate;
import com.xx.happychatboot.middleware.kaptcha.service.KaptchaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 验证码controller
 */
@Controller
public class KaptchaController {
    private final Producer producer;

    /**
     * 使用构造函数注入 --- 面相接口编程思想 如需指定顶层实现，可以通过@Qualifier注解指定
     * @param producer
     */
    @Autowired
    public KaptchaController(Producer producer) {
        this.producer = producer;
    }

    @Autowired
    private KaptchaService kaptchaService;

    /**
     * 前后端分离获取验证码
     *
     * @param response
     * @param session
     * @throws IOException
     */
    @GetMapping("/captcha")
    @ResponseBody
    public ResponseDate getVerifyCode(HttpServletResponse response, HttpSession session) throws IOException {
        return kaptchaService.getVerifyCode(response, session, producer);
    }
}
